package com.jysz.certificate.service.cm.applyCer;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.jysz.certificate.persistence.dao.cm.ApplyCer.ApplyCerDao;
import com.jysz.certificate.persistence.enums.applyAdd.ApplyAddType;
import com.jysz.certificate.persistence.model.cm.applyAdd.ApplyCer;
import com.jysz.certificate.persistence.model.cm.applyAdd.ApplyCerDTO;
import com.jysz.certificate.persistence.model.cs.TheNetwork;
import com.jysz.certificate.utils.DateUtil;
import com.jysz.webcore.ssh.orm.hibernate.support.Page;
import com.jysz.webcore.ssh.service.EntityService;

@Service
public class ApplyCerService extends EntityService<ApplyCer, Long, ApplyCerDao>  {

	@Resource
	private ApplyCerDao applyCerDao;
	
	@Override
	public ApplyCerDao getEntityDao() {
		// TODO Auto-generated method stub
		return applyCerDao;
	}
	
	public Page<ApplyCer> findPage(ApplyCerDTO search ,int pageNo, int pageSize){
		Criteria criteria = getEntityDao().createCriteria();
		Criteria shopCriteria = null;
		if(null != search){
			if(StringUtils.isNotBlank(search.getUserName())){
				criteria.createCriteria("user").add(Restrictions.eq("realName", search.getUserName()));
			}
			if(StringUtils.isNotBlank(search.getNameShop())){
				shopCriteria = criteria.createCriteria("shop");
				shopCriteria.add(Restrictions.eq("shopName", search.getNameShop()));
			}
			if(search.getApplyDate() !=null ){
				criteria.add(Restrictions.gt("applyDate", search.getApplyDate()));
			}
			if(search.getApplyEndDate() !=null ){
				criteria.add(Restrictions.le("applyDate",  DateUtil.addDays(search.getApplyEndDate(),1)));
			}
			if(search.getState() != null && search.getState()>=0){
				criteria.add(Restrictions.eq("state", ApplyAddType.index(search.getState())));
			}
		}
		
		if(shopCriteria == null){
			applyCerDao.addShopCriteria(criteria, false, null);
		}else{
			applyCerDao.addShopCriteria(shopCriteria, true, null);
		}
		
		
		return this.findPage(criteria, pageNo, pageSize);
	}

}
